robustness_balance <- function(
  data = data, balance_table
) {

  # initial binding of globals
  `%>%` <- magrittr::`%>%`
  election <- relative_date <- win_cabinet <- elecdist <- NULL
  household_income <- NULL

  # Netherlands -------------------------------------------------------------

  # before the cutoff date
  nld <- data %>%
    dplyr::filter(election == "Netherlands 2012 general election") %>%
    dplyr::filter(relative_date != 0) %>%
    dplyr::filter(relative_date > -21 & relative_date < 109) %>%
    dplyr::mutate(
      elecdist = as.character(elecdist),
      elecdist = as.factor(elecdist)
    ) %>%
    dplyr::mutate(household_income = stringr::str_extract(
      string = household_income, pattern = "\\d+"
    )) %>%
    dplyr::mutate(household_income = as.numeric(household_income)) %>%
    dplyr::mutate(edu1 = dplyr::case_when(
      education == "1" ~ 1,
      education != "1" ~ 0
    )) %>%
    dplyr::mutate(edu2 = dplyr::case_when(
      education == "2" ~ 1,
      education != "2" ~ 0
    )) %>%
    dplyr::mutate(edu3 = dplyr::case_when(
      education == "3" ~ 1,
      education != "3" ~ 0
    )) %>%
    dplyr::mutate(edu4 = dplyr::case_when(
      education == "4" ~ 1,
      education != "4" ~ 0
    )) %>%
    dplyr::mutate(edu5 = dplyr::case_when(
      education == "5" ~ 1,
      education != "5" ~ 0
    )) %>%
    dplyr::mutate(dist1 = dplyr::case_when(
      elecdist == "NL11" | elecdist == "NL12" | elecdist == "NL13" ~ 1,
      elecdist != "NL11" & elecdist != "NL12" & elecdist != "NL13" ~ 0
    )) %>%
    dplyr::mutate(dist2 = dplyr::case_when(
      elecdist == "NL21" | elecdist == "NL22" | elecdist == "NL23" ~ 1,
      elecdist != "NL21" & elecdist != "NL22" & elecdist != "NL23" ~ 0
    )) %>%
    dplyr::mutate(dist3 = dplyr::case_when(
      elecdist == "NL31" | elecdist == "NL32" | elecdist == "NL33" |
        elecdist == "NL34" ~ 1,
      elecdist != "NL31" & elecdist != "NL32" & elecdist != "NL33" &
        elecdist != "NL34" ~ 0
    )) %>%
    dplyr::mutate(dist4 = dplyr::case_when(
      elecdist == "NL41" | elecdist == "NL42" ~ 1,
      elecdist != "NL41" & elecdist != "NL42" ~ 0
    ))

  ## quick tests
  glm(gov_formed ~ win_cabinet + female + age + household_income + education
      + left_right_self + elecdist,
      data = nld) %>%
    summary(.)

  ## electoral status
  elect_status <- balance_table(
    dv = "win_cabinet", data = nld, label = "Electoral status"
  )

  ## female
  female <- balance_table(dv = "female", data = nld, label = "Female")

  ## age
  age <- balance_table(dv = "age", data = nld, label = "Age")

  ## household income
  income <- balance_table(dv = "household_income", data = nld, label = "Income")

  ## household income missing
  na_income <- balance_table(dv = "hhinc_na", data = nld, label = "Income NA")

  ## education category 1
  edu1 <- balance_table(dv = "edu1", data = nld, label = "Edu 1")

  ## education category 2
  edu2 <- balance_table(dv = "edu2", data = nld, label = "Edu 2")

  ## education category 3
  edu3 <- balance_table(dv = "edu3", data = nld, label = "Edu 3")

  ## education category 4
  edu4 <- balance_table(dv = "edu4", data = nld, label = "Edu 4")

  ## education category 5
  edu5 <- balance_table(dv = "edu5", data = nld, label = "Edu 5")

  ## left_right_self
  lr <- balance_table(dv = "left_right_self", data = nld, label = "Left-right")

  ## dist 1
  dist1 <- balance_table(dv = "dist1", data = nld, label = "Dist 1")

  ## dist 2
  dist2 <- balance_table(dv = "dist2", data = nld, label = "Dist 2")

  ## dist 3
  dist3 <- balance_table(dv = "dist3", data = nld, label = "Dist 3")

  ## dist 4
  dist4 <- balance_table(dv = "dist4", data = nld, label = "Dist 4")

  # combine everything to table
  balance_nld <- rbind(
    elect_status, female, age, income, na_income, edu1, edu2, edu3, edu4, edu5,
    lr, dist1, dist2, dist3, dist4
  )
  write.csv(
    x = balance_nld, file = "./tables/table_S-B1.csv", row.names = FALSE
  )

  # Norway ------------------------------------------------------------------
  nor <- data %>%
    dplyr::filter(election == "Norway 1997 general election") %>%
    dplyr::filter(relative_date != 0) %>%
    dplyr::filter(relative_date > -24 & relative_date < 18) %>%
    dplyr::mutate(
      elecdist = as.character(elecdist),
      elecdist = as.factor(elecdist)
    ) %>%
    dplyr::mutate(income = as.character(household_income)) %>%
    dplyr::mutate(HHinc_Q20 = dplyr::case_when(
      household_income == "1" ~ 1,
      household_income != "1" ~ 0
    )) %>%
    dplyr::mutate(HHinc_Q40 = dplyr::case_when(
      household_income == "2" ~ 1,
      household_income != "2" ~ 0
    )) %>%
    dplyr::mutate(HHinc_Q60 = dplyr::case_when(
      household_income == "3" ~ 1,
      household_income != "3" ~ 0
    )) %>%
    dplyr::mutate(HHinc_Q80 = dplyr::case_when(
      household_income == "4" ~ 1,
      household_income != "4" ~ 0
    )) %>%
    dplyr::mutate(HHinc_Q100 = dplyr::case_when(
      household_income == "5" ~ 1,
      household_income != "5" ~ 0
    )) %>%
    dplyr::mutate(edu3 = dplyr::case_when(
      education == 3 ~ 1,
      education != 3 ~ 0
    )) %>%
    dplyr::mutate(edu4 = dplyr::case_when(
      education == 4 ~ 1,
      education != 4 ~ 0
    )) %>%
    dplyr::mutate(edu5 = dplyr::case_when(
      education == 5 ~ 1,
      education != 5 ~ 0
    )) %>%
    dplyr::mutate(edu8 = dplyr::case_when(
      education == 8 ~ 1,
      education != 8 ~ 0
    )) %>%
    dplyr::mutate(nor1 = dplyr::case_when(
      elecdist == "NOR1" ~ 1,
      elecdist != "NOR1" ~ 0
    )) %>%
    dplyr::mutate(nor2 = dplyr::case_when(
      elecdist == "NOR2" ~ 1,
      elecdist != "NOR2" ~ 0
    )) %>%
    dplyr::mutate(nor3 = dplyr::case_when(
      elecdist == "NOR3" ~ 1,
      elecdist != "NOR3" ~ 0
    )) %>%
    dplyr::mutate(nor4 = dplyr::case_when(
      elecdist == "NOR4" ~ 1,
      elecdist != "NOR4" ~ 0
    )) %>%
    dplyr::mutate(nor5 = dplyr::case_when(
      elecdist == "NOR5" ~ 1,
      elecdist != "NOR5" ~ 0
    )) %>%
    dplyr::mutate(nor6 = dplyr::case_when(
      elecdist == "NOR6" ~ 1,
      elecdist != "NOR6" ~ 0
    ))

  ## quick tests
  # winners
  glm(gov_formed ~ win_cabinet + female + age + household_income + education
      + left_right_self + elecdist,
      data = nor) %>%
    summary(.)

  ## electoral status
  elect_status <- balance_table(
    dv = "win_cabinet", data = nor, label = "Electoral status"
  )

  ## female
  female <- balance_table(dv = "female", data = nor, label = "Female")

  ## age
  age <- balance_table(dv = "age", data = nor, label = "Age")

  ## household income quintile 0-20
  incomeQ1 <- balance_table(dv = "HHinc_Q20", data = nor, label = "Income_Q20")

  ## household income quintile 20-40
  incomeQ2 <- balance_table(dv = "HHinc_Q40", data = nor, label = "Income_Q40")

  ## household income quintile 40-60
  incomeQ3 <- balance_table(dv = "HHinc_Q60", data = nor, label = "Income_Q60")

  ## household income quintile 60-80
  incomeQ4 <- balance_table(dv = "HHinc_Q80", data = nor, label = "Income_Q80")

  ## household income quintile 60-80
  incomeQ5 <- balance_table(
    dv = "HHinc_Q100", data = nor, label = "Income_Q100"
  )

  ## education category 3
  edu3 <- balance_table(dv = "edu3", data = nor, label = "Edu 3")

  ## education category 4
  edu4 <- balance_table(dv = "edu4", data = nor, label = "Edu 4")

  ## education category 5
  edu5 <- balance_table(dv = "edu5", data = nor, label = "Edu 5")

  ## education category 8
  edu8 <- balance_table(dv = "edu8", data = nor, label = "Edu 8")

  ## left_right_self
  lr <- balance_table(dv = "left_right_self", data = nor, label = "Left-right")

  ## region 1: OSLOFJORD
  nor1 <- balance_table(dv = "nor1", data = nor, label = "Dist 1")

  ## region 2: INNER EAST OF NORWAY
  nor2 <- balance_table(dv = "nor2", data = nor, label = "Dist 2")

  ## region 3: SOUTHERN NORWAY
  nor3 <- balance_table(dv = "nor3", data = nor, label = "Dist 3")

  ## region 4: WESTERN NORWAY
  nor4 <- balance_table(dv = "nor4", data = nor, label = "Dist 4")

  ## region 5: TRANDELAG
  nor5 <- balance_table(dv = "nor5", data = nor, label = "Dist 5")

  ## region 6: NORTHERN NORWAY
  nor6 <- balance_table(dv = "nor6", data = nor, label = "Dist 6")

  # combine everything to table
  balance_nor <- rbind(
    elect_status, female, age, incomeQ1, incomeQ2, incomeQ3, incomeQ4, incomeQ5,
    edu3, edu4, edu5, edu8, lr, nor1, nor2, nor3, nor4, nor5, nor6
  )
  write.csv(
    x = balance_nor, file = "./tables/table_S_B2.csv", row.names = FALSE
  )


  # Iceland -----------------------------------------------------------------
  ice <- data %>%
    dplyr::filter(election == "Iceland 2017 general election") %>%
    dplyr::filter(relative_date != 0) %>%
    dplyr::filter(relative_date > - 15 & relative_date < 21) %>%
    dplyr::mutate(
      elecdist = as.character(elecdist),
      elecdist = as.factor(elecdist)
    ) %>%
    dplyr::mutate(household_income = stringr::str_extract(
      string = household_income, pattern = "\\d+"
    )) %>%
    dplyr::mutate(household_income = as.numeric(household_income)) %>%
    dplyr::mutate(household_income = as.numeric(household_income)) %>%
    dplyr::mutate(income = as.character(household_income)) %>%
    dplyr::mutate(HHinc_Q20 = dplyr::case_when(
      household_income == "1" ~ 1,
      household_income != "1" ~ 0
    )) %>%
    dplyr::mutate(HHinc_Q40 = dplyr::case_when(
      household_income == "2" ~ 1,
      household_income != "2" ~ 0
    )) %>%
    dplyr::mutate(HHinc_Q60 = dplyr::case_when(
      household_income == "3" ~ 1,
      household_income != "3" ~ 0
    )) %>%
    dplyr::mutate(HHinc_Q80 = dplyr::case_when(
      household_income == "4" ~ 1,
      household_income != "4" ~ 0
    )) %>%
    dplyr::mutate(HHinc_Q100 = dplyr::case_when(
      household_income == "5" ~ 1,
      household_income != "5" ~ 0
    )) %>%
    dplyr::mutate(income_na = dplyr::case_when(
      household_income > "5" ~ 1,
      household_income < "6" ~ 0
    )) %>%
    dplyr::mutate(edu3 = dplyr::case_when(
      education == "3" ~ 1,
      education != "3" ~ 0
    ))  %>%
    dplyr::mutate(ed4 = dplyr::case_when(
      education == "4" ~ 1,
      education != "4" ~ 0
    ))  %>%
    dplyr::mutate(edu5 = dplyr::case_when(
      education == "5" ~ 1,
      education != "5" ~ 0
    ))  %>%
    dplyr::mutate(edu6 = dplyr::case_when(
      education == "6" ~ 1,
      education != "6" ~ 0
    ))  %>%
    dplyr::mutate(edu7 = dplyr::case_when(
      education == "7" ~ 1,
      education != "7" ~ 0
    ))  %>%
    dplyr::mutate(edu8 = dplyr::case_when(
      education == "8" ~ 1,
      education != "8" ~ 0
    )) %>%
    dplyr::mutate(dist1 = dplyr::case_when(
      elecdist == "ICE1" ~ 1,
      elecdist != "ICE1" ~ 0
    )) %>%
    dplyr::mutate(dist2 = dplyr::case_when(
      elecdist == "ICE2" ~ 1,
      elecdist != "ICE2" ~ 0
    )) %>%
    dplyr::mutate(dist4 = dplyr::case_when(
      elecdist == "ICE4" ~ 1,
      elecdist != "ICE4" ~ 0
    )) %>%
    dplyr::mutate(dist5 = dplyr::case_when(
      elecdist == "ICE5" ~ 1,
      elecdist != "ICE5" ~ 0
    )) %>%
    dplyr::mutate(dist6 = dplyr::case_when(
      elecdist == "ICE6" ~ 1,
      elecdist != "ICE6" ~ 0
    )) %>%
    dplyr::mutate(dist7 = dplyr::case_when(
      elecdist == "ICE7" ~ 1,
      elecdist != "ICE7" ~ 0
    ))

  ## quick tests
  # winners
  glm(gov_formed ~ win_cabinet + female + age + household_income + education
      + left_right_self + elecdist,
      data = ice) %>%
    summary(.)

  ## walk through vars

  ## electoral status
  elect_status <- balance_table(
    dv = "win_cabinet", data = ice, label = "Electoral status"
  )

  ## female
  female <- balance_table(dv = "female", data = ice, label = "Female")

  ## age
  age <- balance_table(dv = "age", data = ice, label = "Age")

  ## household income quintile 0-20
  incomeQ1 <- balance_table(dv = "HHinc_Q20", data = ice, label = "Income 1")

  ## household income quintile 20-40
  incomeQ2 <- balance_table(dv = "HHinc_Q40", data = ice, label = "Income 2")

  ## household income quintile 40-60
  incomeQ3 <- balance_table(dv = "HHinc_Q60", data = ice, label = "Income 3")

  ## household income quintile 60-80
  incomeQ4 <- balance_table(dv = "HHinc_Q80", data = ice, label = "Income 4")

  ## household income quintile 80-100
  incomeQ5 <- balance_table(dv = "HHinc_Q100", data = ice, label = "Income 5")

  ## Income NA
  income_na <- balance_table(dv = "income_na", data = ice, label = "Income NA")

  ## Edu 3
  edu3 <- balance_table(dv = "income_na", data = ice, label = "Edu 3")

  ## Edu 4
  edu4 <- balance_table(dv = "income_na", data = ice, label = "Edu 4")

  ## Edu 5
  edu5 <- balance_table(dv = "income_na", data = ice, label = "Edu 5")

  ## Edu 6
  edu6 <- balance_table(dv = "income_na", data = ice, label = "Edu 6")

  ## Edu 7
  edu7 <- balance_table(dv = "income_na", data = ice, label = "Edu 7")

  ## Edu 8
  edu8 <- balance_table(dv = "income_na", data = ice, label = "Edu 8")

  ## left-right
  lr <- balance_table(dv = "left_right_self", data = ice, label = "Left-right")

  ## Dist 1
  dist1 <- balance_table(dv = "dist1", data = ice, label = "Dist 1")

  ## Dist 2
  dist2 <- balance_table(dv = "dist2", data = ice, label = "Dist 2")

  ## Dist 4
  dist4 <- balance_table(dv = "dist4", data = ice, label = "Dist 4")

  ## Dist 5
  dist5 <- balance_table(dv = "dist5", data = ice, label = "Dist 5")

  ## Dist 6
  dist6 <- balance_table(dv = "dist6", data = ice, label = "Dist 6")

  ## Dist 7
  dist7 <- balance_table(dv = "dist7", data = ice, label = "Dist 7")

  # combine everything to table
  balance_ice <- rbind(
    elect_status, female, age, incomeQ1, incomeQ2, incomeQ3, incomeQ4, incomeQ5,
    income_na, edu3, edu4, edu5, edu6, edu7, edu8, lr, dist1, dist2, dist4,
    dist5, dist6, dist7
  )
  write.csv(
    x = balance_ice, file = "./tables/table_S_B3.csv", row.names = FALSE
  )
}
